package com.huike.review.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.mapper.MybatisReviewMapper;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * mybatis复习使用的业务层
 * 注意该业务层和我们系统的业务无关，主要是让同学们快速熟悉系统的
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private MybatisReviewMapper reviewMapper;


    /**保存数据的方法*/
    @Override
    public AjaxResult insertReview(Review review) {

        System.err.println(review);
        int insert = reviewMapper.insert(review);
        System.err.println(insert);
        return AjaxResult.success("成功插入:"+insert+"条数据");

    }





    /**删除数据*/
    @Override
    public AjaxResult deleteReview(Integer id) {
        int delete = reviewMapper.delete(id);

        return AjaxResult.success("成功删除:"+delete+"条数据");

    }



    /**修改数据*/
    @Override
    public AjaxResult updateReview(Review review) {
        review.setUpdateBy("2");
        review.setUpdateTime(new Date());
        int update = reviewMapper.update(review);

        return AjaxResult.success("成功修改:"+update+"条数据");

    }




    /**查询数据的方法*/
    @Override
    public AjaxResult selectById(Integer id) {
        Review review = reviewMapper.selectById(id);
        return AjaxResult.success("搜索成功", review);
    }

    @Override
    public AjaxResult select(Integer page, Integer pageSize) {

        Page<Object> objects = PageHelper.startPage(page, pageSize);
        reviewMapper.select();
        long total = objects.getTotal();
        List<Object> result = objects.getResult();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows",result);
        return AjaxResult.success("操作成功", map);
    }


}
